package routers

import (
	"encoding/json"
	"net/http"

	"shqsoft.cn/pasnj/base"

	"github.com/labstack/echo"
)

// DataProcessRouter 数据处理路由
func DataProcessRouter(c echo.Context) (err error) {
	var dateString string
	jsonStr := c.FormValue("Date")
	err = json.Unmarshal([]byte(jsonStr), &dateString)
	if err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	err = base.DB.Exec("UPDATE T_CONF_SYDT SET ISPS=1,ISDP=1").Error
	if dateString != "" {
		err = base.DB.Exec("EXEC UP_DIVI_PROCESS ?", dateString).Error
	} else {
		err = base.DB.Exec("EXEC UP_DIVI_PROCESS").Error
	}
	if err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	// path := base.Context.DataPKG.Path
	// info, err := exec.Command("cmd.exe", "/c", fmt.Sprintf(`DTEXEC.EXE /F %s`, path)).Output()
	// enc := mahonia.NewEncoder("GB2312")
	// info2 := enc.ConvertString(string(info))
	// if err != nil {
	// 	log.Print(info2)
	// 	return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	// }
	err = base.DB.Exec("EXEC msdb.dbo.sp_start_job 'MNLRDP'").Error
	if err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	return c.String(http.StatusOK, "OK")
}

// DataProcessRouter 数据处理路由
func ReImportRouter(c echo.Context) (err error) {
	var dateString string
	jsonStr := c.FormValue("Date")
	err = json.Unmarshal([]byte(jsonStr), &dateString)
	if err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	err = base.DB.Exec("UPDATE T_CONF_SYDT SET RJB_SYDT=?", dateString).Error
	if err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	err = base.DB.Exec("EXEC msdb.dbo.sp_start_job 'RJB_IMPORT'").Error
	if err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	return c.JSON(http.StatusOK, "OK")
}

// CreateDataRouter 数据处理路由
func CreateDataRouter(c echo.Context) (err error) {
	var dateString string
	jsonStr := c.FormValue("Date")
	err = json.Unmarshal([]byte(jsonStr), &dateString)
	if err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	err = base.DB.Exec("EXEC UP_BASE_SCSJ ?", dateString).Error
	if err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	return c.JSON(http.StatusOK, "OK")
}
